package cn.com.nes.site.action.user;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.AdService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

@Controller("ad")
@RequestMapping("/ad")
public class AdAction {

    @Resource
    private AdService service;

    //热链云获取广告列表
    @RequestMapping(method = RequestMethod.POST, value = "/getAdList")
    public @ResponseBody
    PageData getAdList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.getAdList(req) ;
        return res;
    }

    //热链云保存广告信息
    @RequestMapping(method = RequestMethod.POST, value = "/saveAd")
    public @ResponseBody
    PageData saveAd(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws SQLException {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.saveAd(reqStatus.getReq());
        return res;
    }

    //热链云删除广告信息
    @RequestMapping(method = RequestMethod.POST, value = "/delAd")
    public @ResponseBody
    PageData delAd(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws SQLException {
        DataRes reqStatus = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (reqStatus.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(reqStatus.getRetCode());
            res.setRetMessage(reqStatus.getRetMessage());
            return res;
        }
        res = service.delAd(reqStatus.getReq());
        return res;
    }

    //小程序获取对应广告
    @RequestMapping(method = RequestMethod.GET, value = "/getAd")
    public @ResponseBody
    PageData getAd(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.getAd(req) ;
        return res;
    }

}